package bookManageSystem.view;

import bookManageSystem.po.Employee;
import bookManageSystem.po.Order;
import bookManageSystem.service.EmployeeService;
import bookManageSystem.service.OrderService;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.List;

public class EmployeeView extends JPanel {

    private EmployeeService employeeService = new EmployeeService();
    private DefaultTableModel orderTableModel;
    private JTable orderTable;
    public EmployeeView() {
        // 设置面板布局
        setLayout(new BorderLayout());

        initializeTable();
        loadOrders();

        // 可以将 orderTable 添加到面板中，例如放到中心位置
        add(new JScrollPane(orderTable), BorderLayout.CENTER);
    }

    private void initializeTable() {

        // 仅初始化一次表格结构
        if (orderTableModel == null) {
            orderTableModel = new DefaultTableModel(
                    new String[]{"用户编号", "姓名", "密码", "手机号", "余额"}, 0);
            orderTable = new JTable(orderTableModel);
        }
    }

    private void loadOrders() {
        List<Employee> allEmployees = employeeService.getAllEmployees();

        orderTableModel.setRowCount(0); // 清空表格
        for (Employee employee : allEmployees) {
            orderTableModel.addRow(new Object[]{
                    employee.getUserId(),
                    employee.getUsername(),
                    employee.getPassword(),
                    employee.getPhone(),
                    employee.getBalance()
            });
        }
    }

    public void getOrderView() {
        // 如果需要在特定事件下刷新订单视图，可以调用 loadOrders 方法
        loadOrders();
    }

}
